package com.example.orderserver.service;


import com.example.orderserver.dao.OrderDao;
import com.example.orderserver.dao.OrderDetailetDao;
import com.example.orderserver.entities.Order;
import com.example.orderserver.entities.OrderDetailet;

import com.example.server_engineer.service.AbstractTypedService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * @author Administrator
 */
@Slf4j
@Service
public class OrderService
        extends AbstractTypedService<Order, Integer> {

    OrderDao orderDao;

    OrderService(OrderDao dao) {
        this.dataContext = dao;
        this.orderDao = dao;
    }

    public List<Order> queryAll() {
        List<Order> lst = null;
        try {
            lst = ((OrderDao) this.dataContext).findAll();
            log.info("find orders: " + lst.size());
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return lst;
    }

    @Resource
    OrderDetailetDao orderDetailetDao;

    @Resource
    UserFeignService ufsvc;
    @Resource
    FoodFeignService foodFeignService;

    @Resource
    DeliveryFeignService dfsvc;

    public Order placeOrder(Long userid, String foodname, Integer quantity, Integer daid) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 获取菜品
        Order newOrder = new Order();
        try {
//            food = foodDao.findFoodByName(foodname);
//            log.info(String.valueOf(food));
//            newOrder.setBusinessId(food.getBusinessId());
            newOrder.setBusinessId((Integer) foodFeignService.queryBusinessIdByName(foodname).getData());
        } catch (Exception e) {
            log.info(e.toString());
        }

        // 检查用户合法性
        if (ufsvc.findById(userid).getData()!=null) {
            newOrder.setUserId(String.valueOf(userid));
        } else {
            log.error("用户不存在");
        }
        if (dfsvc.queryAddressById(daid).getData()!=null) {
            newOrder.setDaId(daid);
        } else {
            log.error("配送地址不存在");
        }
        newOrder.setDate(String.valueOf(new Date()));

        // 保存订单
        orderDao.save(newOrder);

        //添加订单详情
        OrderDetailet newod = new OrderDetailet();
        newod.setOrderId(newOrder.getId());

//
        Object food = foodFeignService.queryByName(foodname).getData();
        Class<?> foodClass = food.getClass();
        Method getIdMethod = foodClass.getMethod("getId");
        Integer foodId = (Integer) getIdMethod.invoke(food);
        newod.setFoodId(foodId);
        newod.setQuantity(quantity);
        newod.setLast_modified_date(new Date());
        orderDetailetDao.save(newod);
//        System.out.println(newod.toString());
        return newOrder;
    }


//     查找状态为state的订单
//    public List<OrderDto> findOrder(String userid, int state) {
//        List<OrderDto> list = new ArrayList<>();
//        // 先找到该用户的所有订单
//        List<Order> list1 = orderDao.findOrderByUserId(userid);
//        for (Order order : list1) {
//            OrderDto orderDto = new OrderDto();
//            if (order.getState() == state) {
//                // state = 0 未支付  state = 1 已支付
//                Optional<Business> business = businessDao.findBusinessById(order.getBusinessId());
//
//                orderDto.setBusinessName(business.get().getName());
//                orderDto.setDeliveryPrice(business.get().getDelivery_price());
//
//                orderDto.setTotalPrice(order.getTotal());
//                // 根据orderid查 order_detail
//                List<OrderDetailet> od = orderDetailetDao.findOrderDetailetByOrderId(order.getId());
//                List<FoodDto> foodDtoList = new ArrayList<>();
//                for (OrderDetailet detail : od) {
//                    FoodDto fd = new FoodDto();
//                    Food food = foodDao.findFoodById(detail.getFoodId());
//                    fd.setFoodname(food.getName());
//                    fd.setQuantity(detail.getQuantity());
//                    fd.setPrice(food.getPrice());
//                    foodDtoList.add(fd);
//                }
//                orderDto.setFoodlist(foodDtoList);
//                list.add(orderDto);
//            }
//        }
//        return list;
//    }
    public Page<Order> queryOrderByPage(Pageable pageable) {
        return orderDao.queryPage(pageable, null);
    }
}
